﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using hlavny.modul1;

namespace hlavny.modul2
{
    [Serializable]
    public class CSysTables
    {
        private List<CTable> tables = null;
        private int lastId = 0;
        [NonSerialized]
        private CTable currentTable = null;

        #region CSysTables constructor

        public CSysTables()
        {
            tables = new List<CTable>();
        }

        #endregion

        #region CSysTables properties

        public List<CTable> Tables
        {
            get { return tables; }
            set { tables = value; }
        }
        public int LastId
        {
            get { return lastId; }
            set { lastId = value; }
        }
        public CTable CurrentTable
        {
            get { return currentTable; }
            set { currentTable = value; }
        }

        #endregion

        #region Help functions

        /// <summary>
        /// Fuction search for table id and return index of table in sysTables
        /// </summary>
        /// <param name="id">id of the table</param>
        /// <returns>index of table in sysTables, if not found returns negative number</returns>
        public int GetTableIndex(int id)
        {
            return tables.BinarySearch(new CTable(id));
        }

        #endregion

        #region CSysTables main functions

        /// <summary>
        /// Function create a new table
        /// </summary>
        /// <param name="name">name of the table</param>
        /// <param name="database">id of the table database</param>
        /// <returns>object CTable if is it created, otherwise false</returns>
        public CTable CreateTable(string name, int database)
        {
            lastId++;
            CTable table = new CTable(lastId, database, name);
            tables.Add(table);
            tables.Sort();
            Save();
            return table;
        }

        /// <summary>
        /// Function drop table
        /// </summary>
        /// <param name="table">table to drop</param>
        /// <returns>true if table droped, otherwise false</returns>
        public bool DropTab(CTable table)
        {
            int index = GetTableIndex(table.Id);
            if (index < 0)
            {
                return false;
            }
            else
            {
                tables.RemoveAt(index);
                tables.Sort();
                Save();
            }
            return true;
        }

        /// <summary>
        /// Function add column for current table
        /// </summary>
        /// <param name="column">column to add</param>
        /// <returns>true if column addeing done, otherwise false</returns>
        public bool AddAttr(CAttribute column)
        {
            if (currentTable != null)
            {
                currentTable.AddAttr(column);
                currentTable.ColumnsIds.Sort();
                Save();
                return true;
            }
            else
            {
                return false;
            }

        }

        /// <summary>
        /// Drop column from current table
        /// </summary>
        /// <param name="attribute">attribute to drop</param>
        /// <returns>true if column droped, otherwise false</returns>
        public bool DrobAtr(CAttribute attribute)
        {
            int index = currentTable.ColumnsIds.BinarySearch(attribute.Id);
            if (index < 0)
            {
                return false;
            }
            else
            {
                currentTable.ColumnsIds.RemoveAt(index);
                currentTable.ColumnsIds.Sort();
                Save();
                return true;
            }
        }
        
        #endregion

        #region Save functions

        /// <summary>
        /// Function serialize all table sysTables to the file
        /// </summary>
        public void Save()
        {
            CFunctions.StoreSysTables(this);
        }

        #endregion

        

        
    }
}
